<?php
namespace app\index\controller;
use caretree\Caretree;
class Index extends Base
{
    public function index()
    {
		// echo session('level_id');
		// echo session('level_rate');
		// encryption();  公共函数加密
		// echo $this->config['cache_time'];
		// die;
		//调用公告及促销文章
		if(cache('anmentRes')){
			$anmentRes=cache('anmentRes');
		}else{
			$anmentRes = model('article')->getArts(11,3);//公告
			if($this->config['cache'] == '是'){
				cache('anmentRes',$anmentRes,$this->config['cache_time']);
			}
		}
		if(cache('salesRes')){
			$salesRes=cache('salesRes');
		}else{
			$salesRes = model('article')->getArts(17,3); //促销
			
			if($this->config['cache'] == '是'){
				cache('salesRes',$salesRes,$this->config['cache_time']);
			}
		}
		if(cache('categoryRes')){
			$categoryRes = cache('categoryRes');
		}else{
		$categoryRes = model('category')->getRecCategorys(5,0);  //首页推荐  推荐位的顶级分类 查的的是顶级分类下的手机通讯和家用电器
		foreach($categoryRes as $k => $v){
			//获取顶级分类下被设为 首页推荐的二级分类
			$categoryRes[$k]['children'] = model('category')->getRecCategorys(5,$v['id']);
			//获取二级栏目及其子栏目下的精品推荐商品,用于首页展示
			foreach ($categoryRes[$k]['children'] as $k1 =>$v1){
				//获取精品推荐
				//1.获取当前主分类下所有的子分类id
				 $categoryRes[$k]['children'][$k1]['bestGoods'] = model('Goods')->getIndexRecposGoods($v1['id'],7);
			}
			
			//获取新品推荐
			$categoryRes[$k]['newGoods'] = model('Goods')->getIndexRecposGoods($v['id'],4);
			//获取该顶级分类相关的品牌信息
			$categoryRes[$k]['brands']=model('category')->getCategoryBrands($v['id']);
			
			//获取当前顶级栏目的左侧图信息
			 $categoryRes[$k]['leftImgs']=model('categoryAd')->getCategoryAd($v['id']);
		}
		if($this->config['cache'] == '是'){
			cache('categoryRes',$categoryRes,$this->config['cache_time']);
		}
		}
		//调用首页商品
		if(cache('indexGoodsRes')){
			$indexGoodsRes = cache('indexGoodsRes');
		}else{
			$indexGoodsRes = model('goods')->getRecposGoods(8,20);
			if($this->config['cache'] == '是'){
				cache('indexGoodsRes',$indexGoodsRes,$this->config['cache_time']);
			}
		}
		//调用首页轮播图
		if(cache('alternateImgRes')){
			$alternateImgRes = cache('alternateImgRes');
		}else{
			$alternateImgRes=model('alternateImg')->getAlterImg();
			if($this->config['cache'] == '是'){
				cache('alternateImgRes',$alternateImgRes,$this->config['cache_time']);
			}
		}
		
		// dump($alternateImgRes);die;
		$this->assign([
		'show_right' => 1, //文章列表和商品列表头部偏移判断
		'show_nav' => 1, //首页导航栏默认展开,其他页面收缩
		'categoryRes'=>$categoryRes, //首页大分类数据
		'indexGoodsRes'=>$indexGoodsRes, //首页商品
		'anmentRes'=>$anmentRes, //公告文章
		'salesRes' =>$salesRes, //促销文章
		'alternateImgRes'=>$alternateImgRes, //轮播图数据
	]);
	return view();
   }
  
}
